"""
@Author  : 康帅
@Time    : 2021/3/10 15:30
@Function: 热水器设计v2.0
"""

from DesignMode.ListenMode.ListenBaseClass import Observable, Observer
"""引入ABCMeta，abstractmethod来定义抽象类和抽象方法"""


class WaterHeater(Observable):
    """ 热水器(继承自封装的基类Observable) """

    def __init__(self):
        """ 实例 """

        super().__init__()
        self.__temperature = 25

    def get_temperature(self):
        """ 获取温度 """

        return self.__temperature

    def set_temperature(self, temperature):
        """ 设置温度 """

        self.__temperature = temperature
        print("当前温度是：" + str(self.__temperature) + "℃")
        self.notify_ob_servers()


class WashingMode(Observer):
    """ 该模式用于洗澡 """

    def update(self, observable, objects):
        if isinstance(observable, WaterHeater) \
                and observable.get_temperature() >= 50 \
                and observable.get_temperature() < 70:
            print("水已经烧好！可以洗澡")


class DrinkingMode(Observer):
    """ 该模式用于饮用 """

    def update(self, observable, objects):
        if isinstance(observable, WaterHeater) \
                and observable.get_temperature() >= 100:
            print("水已经烧好！可以饮用")


if __name__ == '__main__':
    heater = WaterHeater()
    washing_ob_server = WashingMode()
    drinking_ob_server = DrinkingMode()
    heater.add_ob_server(washing_ob_server)
    heater.add_ob_server(drinking_ob_server)
    heater.set_temperature(40)
    heater.set_temperature(60)
    heater.set_temperature(100)
